var menuEdit = {
	menuId:null,
	parentMenuId:null,
	isParentMenu:false
};

$(function(){
	changeActiveItem('menuList');
	initData();
	setData();
	savehandle();
});
/**
 * 提示信息处理
 */
function savehandle(){
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap:{},
		successMsg: '修改成功！',
		failMsg: '修改失败,请重试！',
		success: successCallBack 
	});
}

/**
 * 菜单编辑成功跳转
 */
function successCallBack(){
	location.href = 'menuList.jsp';
}

function initData(){
	var id = location.hash;
	var ids = id.substring(1, id.length).split(',');
	menuEdit.menuId = ids[0]; 
}

/**
*获取初始数据
* 
*/
function setData(){
	var menuId = menuEdit.menuId;
	$.getJSON(
		'/fw/auth/menu_getMenuById.action',
		{
			menuId:menuId
		},
		function(data){
			if(data.rows && data.rows.length){
				var rows = data.rows[0];
				if(rows.url){
					$('#menuEdit #url').val(rows.url);
				}else{
					$('#menuEdit #url').val('');
					menuEdit.isParentMenu =true;
					$(".noParent").hide();
				}
				if(rows.title){
					$('#menuEdit #title').val(rows.title);
				}else{
					$('#menuEdit #title').val('');
				}
				if(rows.aliasName){
					$('#menuEdit #aliasName').val(rows.aliasName);
				}else{
					$('#menuEdit #aliasName').val('');
				}
				if(rows.parentId){
					menuEdit.parentMenuId = rows.parentId;
					$("#menuEdit #parentMenu").val(rows.parentId);
				}else{
					$("#menuEdit #parentMenu").val('');
				}
				getParentMenu();
			}
		}
	);
}

/**
 * 获取父菜单信息
 */
function getParentMenu(){
	$.getJSON('/fw/auth/menu_getParentMenu.action',{},function(data){
		if(data.rows){
			var rows = data.rows;
			var i = 0;
			for(i = 0; i<rows.length;i++){
				var str = "<option value="+rows[i].menuId+">"+rows[i].title+"</option>"
				$("#menuEdit #parentMenu").append(str);
				if(menuEdit.parentMenuId != null){
					$("#menuEdit #parentMenu").val(menuEdit.parentMenuId);
				}
				
			}
		}
	});
}
/**
 * 提交修改信息
 * @return {TypeName} 
 */
function submitMenu() {
	var menuId = menuEdit.menuId;
	var url = $('#menuEdit #url').val();
	var data = null;
	if(!menuEdit.isParentMenu){
		if(null == url || "" == url) {
			$('#alertdiv').savehandle('setmsg',{msg:'菜单链接不能为空!',timer:2000});
			$('#menuEdit #url').focus();
			return false;
		}
		var title = $('#menuEdit #title').val();
		if(null == title || "" == title) {
			$('#alertdiv').savehandle('setmsg',{msg:'菜单名称不能为空!',timer:2000});
			$('#menuEdit #title').focus();
			return false;
		}
		var aliasName = $('#menuEdit #aliasName').val();
		if(null==aliasName || ""==aliasName){
			$('#alertdiv').savehandle('setmsg',{msg:'别名不能为空!',timer:2000});
			$('#menuEdit #aliasName').focus();
			return false;
		}
		var parentMenuId= $("#menuEdit #parentMenu").val();
		if(null ==parentMenuId || ""== parentMenuId){
			$('#alertdiv').savehandle('setmsg',{msg:'父菜单不能为空!',timer:2000});
			return false;
		}
		data = {
				menuId:menuEdit.menuId,
				url:url,
				title:title,
				aliasName:aliasName,
				"menu.menuId":parentMenuId
		}
	}else{
		var title = $('#menuEdit #title').val();
		if(null == title || "" == title) {
			$('#alertdiv').savehandle('setmsg',{msg:'菜单名称不能为空!',timer:2000});
			$('#menuEdit #title').focus();
			return false;
		}
		data = {
				menuId:menuEdit.menuId,
				title:title
		}
	}
	
	
 	$.getJSON(
		'/fw/auth/menu_editMenu.action',
		data,
		function(data){
			$('#alertdiv').savehandle('handle',data,2000);
		}
	);
}

/**
 * 重置菜单信息
 */
function resetData() {
	setData();
}